IKK*_*KKA 6 iphone objective-c uilabel ios line-spacing
我想在iOS中UILabel
通过xib垂直对齐文本与行间距,例如:
hello,
how are
you?
Run Code Online (Sandbox Code Playgroud)
请帮我.
IKK*_*KKA 10
是的,可以通过xib调整行间距.单击xib上的UIlabel,然后将"text"属性类型更改为"Attributed",然后浏览以下图像.然后输入换行(按ctrl-return快捷键)并将光标保持在两行之间,然后调整所需的'line'属性和'Max Height'属性.我的UILael文字是"你好,你好吗?"
创建这个简单的UILabel子类:
@interface CustomLabel : UILabel
@property (assign, nonatomic) CGFloat myLineSpacing;
@end
@implementation CustomLabel
- (void)setMyLineSpacing:(CGFloat)myLineSpacing {
_myLineSpacing = myLineSpacing;
self.text = self.text;
}
- (void)setText:(NSString *)text {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = _myLineSpacing;
paragraphStyle.alignment = self.textAlignment;
NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyle};
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text
attributes:attributes];
self.attributedText = attributedText;
}
Run Code Online (Sandbox Code Playgroud)
myLineSpacing
用IB 设置属性值.
仍然无法在IB中预览,但行间距值在xib中!
您可以label.text
在代码中设置,而无需关心行间距.
注意:
不要使属性名称为" lineSpacing
".UILabel确实有未记录的lineSpacing属性,并且覆盖会破坏某些东西.
小智 -8
在任何文本编辑器(如 TextEdit 等)中按照您想要的方式键入它,然后粘贴到 xib 中标签的文本部分。
但请确保标签的高度更适合内容,并增加行数。对于这种情况,可以是 3 个或更多。
归档时间: |
|
查看次数: |
9592 次 |
最近记录: |