创建uilabel的分类
//竖着显示
.h写入
@property (nonatomic) NSString *verticalText;
.m写入
- (NSString *)verticalText{
// 利用runtime添加属性
return objc_getAssociatedObject(self, @selector(verticalText));
}
- (void)setVerticalText:(NSString *)verticalText{
objc_setAssociatedObject(self, &verticalText, verticalText, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
NSMutableString *str = [[NSMutableString alloc] initWithString:verticalText];
NSInteger count = str.length;
for (int i = 1; i < count; i ++) {
[str insertString:@"\n" atIndex:i*2-1];
}
self.text = str;
self.numberOfLines = 0;
}
用法:label.verticalText = @"竖着";
时间: 2024-11-14 07:32:26