IOS 将文本占位符居中并左对齐
IOS align text placeholder to be in the center and left alignment
我正在创建一个 IOS 表单来提交反馈。
我想让占位符文本在文本字段中垂直居中,水平左对齐
我试过了
- (void)drawPlaceholderInRect:(CGRect)rect {
[RGB(36, 84, 157) setFill];
UIFont *font = [UIFont SingPostLightItalicFontOfSize:_placeholderFontSize fontKey:kSingPostFontOpenSans];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary *attributes = @{ NSFontAttributeName: font,
NSParagraphStyleAttributeName: paragraphStyle,
NSForegroundColorAttributeName: [UIColor blackColor]};
[[self placeholder] drawInRect:rect withAttributes:attributes];
}
但占位符文本位于顶部垂直位置。怎样才能达到金牌。非常感谢
您可以将占位符的 "y" 位置表示您的 "rect" 值作为文本字段 height/2 。如果您使用文本字段的默认 属性 作为占位符,则还有一个解决方案水平对齐。
喜欢:
var tt : UITextField
tt.placeholder = ""
以下代码将解决您的问题。我自己试过了。这只是您的代码的额外一行。
- (void)drawPlaceholderInRect:(CGRect)rect {
CGFloat fontSize = 18;
UIFont *font = [UIFont systemFontOfSize:fontSize];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary *attributes = @{ NSFontAttributeName: font,
NSParagraphStyleAttributeName: paragraphStyle,
NSForegroundColorAttributeName: [UIColor blackColor]};
CGRect newRect = CGRectInset(rect, 0, rect.size.height/2 - fontSize/2);
[[super placeholder] drawInRect:newRect withAttributes:attributes];
}
我正在创建一个 IOS 表单来提交反馈。
我想让占位符文本在文本字段中垂直居中,水平左对齐
我试过了
- (void)drawPlaceholderInRect:(CGRect)rect {
[RGB(36, 84, 157) setFill];
UIFont *font = [UIFont SingPostLightItalicFontOfSize:_placeholderFontSize fontKey:kSingPostFontOpenSans];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary *attributes = @{ NSFontAttributeName: font,
NSParagraphStyleAttributeName: paragraphStyle,
NSForegroundColorAttributeName: [UIColor blackColor]};
[[self placeholder] drawInRect:rect withAttributes:attributes];
}
但占位符文本位于顶部垂直位置。怎样才能达到金牌。非常感谢
您可以将占位符的 "y" 位置表示您的 "rect" 值作为文本字段 height/2 。如果您使用文本字段的默认 属性 作为占位符,则还有一个解决方案水平对齐。
喜欢:
var tt : UITextField
tt.placeholder = ""
以下代码将解决您的问题。我自己试过了。这只是您的代码的额外一行。
- (void)drawPlaceholderInRect:(CGRect)rect {
CGFloat fontSize = 18;
UIFont *font = [UIFont systemFontOfSize:fontSize];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary *attributes = @{ NSFontAttributeName: font,
NSParagraphStyleAttributeName: paragraphStyle,
NSForegroundColorAttributeName: [UIColor blackColor]};
CGRect newRect = CGRectInset(rect, 0, rect.size.height/2 - fontSize/2);
[[super placeholder] drawInRect:newRect withAttributes:attributes];
}