label 文本竖排 仅限汉字
.h
#import
NS_ASSUME_NONNULL_BEGIN
@interfaceUILabel (landscape)
@property(nonatomic) NSString *contentText;
@end
NS_ASSUME_NONNULL_END
.m
#import "UILabel+landscape.h"
#import
@implementationUILabel (landscape)
- (NSString*)contentText
{
return objc_getAssociatedObject(self, @selector(contentText));
}
- (void)setContentText:(NSString*)contentText
{
objc_setAssociatedObject(self, &contentText, contentText, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
NSMutableString*str = [[NSMutableStringalloc]initWithString:contentText];
NSIntegercount = str.length;
for(inti =1; i < count; i ++) {
[strinsertString:@","atIndex:i*2-1];
}
NSArray *arr = [str componentsSeparatedByString:@","];
NSMutableArray *mulArr = [NSMutableArray array];
CGRect rect = [contentText boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, MAXFLOAT)//限制最大的宽度和高度
options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading |NSStringDrawingUsesLineFragmentOrigin//采用换行模式
attributes:@{NSFontAttributeName: [UIFontsystemFontOfSize:12]}//传人的字体字典
context:nil];
intlineLab = rect.size.height/self.font.lineHeight;
for(inti =0; i < lineLab +2; i++) {
[mulArraddObject:@""];
}
for(intj=0; j
for(inti =0; i < arr.count; i++) {
if(i%(mulArr.count)==j) {
NSString*str1 = mulArr[j];
str1 = [NSStringstringWithFormat:@"%@%@",str1,arr[i]];
[mulArrreplaceObjectAtIndex:j withObject:str1];
}
}
}
NSString* str2 =@"";
for(NSString*stringinmulArr) {
str2 = [NSStringstringWithFormat:@"%@\n%@",str2,string];
}
if(str2 .length>2) {
NSString*str3 = [str2substringToIndex:1];
if([str3isEqualToString:@"\n"]) {
str2 = [str2substringFromIndex:1];
}
}
self.text= str2;
self.numberOfLines = 0;
}
@end