preface
- System Api
textAlignment
Position we usually only use left, right and middle three display methods, so write a category to provide more display positions, respectively including upper left, upper right, lower left, lower right, upper middle and lower middle - Adopt the way of Category processing, simple and convenient to use
Making address:KJEmitterView
A brief introduction to Property & API
NS_ASSUME_NONNULL_BEGIN typedef NS_ENUM(NSUInteger, KJLabelTextAlignmentType) { KJLabelTextAlignmentTypeLeft = 0, KJLabelTextAlignmentTypeRight, KJLabelTextAlignmentTypeCenter, KJLabelTextAlignmentTypeLeftTop, KJLabelTextAlignmentTypeRightTop, KJLabelTextAlignmentTypeLeftBottom, KJLabelTextAlignmentTypeRightBottom, KJLabelTextAlignmentTypeTopCenter, KJLabelTextAlignmentTypeBottomCenter, }; @interface UILabel (KJExtension) @property(Nonatomic,assign)KJLabelTextAlignmentType kTextAlignmentType; / / / to get highly - (CGFloat) kj_calculateHeightWithWidth (CGFloat) width; Given line / / / get high, high - (CGFloat) kj_calculateHeightWithWidth: (CGFloat) width OneLineHeight (CGFloat) height; / / / for text size + (CGSize) kj_calculateLabelSizeWithTitle: (nsstrings *) the title font: (UIFont *) the font constrainedToSize (CGSize) size lineBreakMode:(NSLineBreakMode)lineBreakMode; @end NS_ASSUME_NONNULL_ENDCopy the code
1. KTextAlignmentType sets the text position,
Note that there is an internal textAlignment property that is used by the system, so calling this property does not require the textAlignment property
Use the sample
CGFloat x,y; CGFloat sp = kAutoW(10); CGFloat w = (kScreenW-sp*4)/3.; CGFloat h = w*9/16; NSArray * names = @ [@ "on the left," @ "right," @ "intermediate," @ "left," @ "right," @ "left," @ "right," @ "upper" @ "lower-middle"]. NSInteger types[9] = { KJLabelTextAlignmentTypeLeft, KJLabelTextAlignmentTypeRight, KJLabelTextAlignmentTypeCenter, KJLabelTextAlignmentTypeLeftTop, KJLabelTextAlignmentTypeRightTop, KJLabelTextAlignmentTypeLeftBottom, KJLabelTextAlignmentTypeRightBottom, KJLabelTextAlignmentTypeTopCenter, KJLabelTextAlignmentTypeBottomCenter }; for (int k=0; k<names.count; k++) { x = k%3*(w+sp)+sp; y = k/3*(h+sp)+sp+64+sp*2; UILabel *label = [UILabel kj_createLabelWithText:names[k] FontSize:16 TextColor:UIColor.orangeColor]; Label the backgroundColor = [UIColor orangeColor colorWithAlphaComponent: 0.2]; label.kTextAlignmentType = types[k]; label.borderWidth = 1; label.borderColor = UIColor.orangeColor; label.frame = CGRectMake(x, y, w, h); [self.view addSubview:label]; }Copy the code
Category
// // UILabel+ KJextension. m // KJEmitterView // // Created by Yang Kejun on 2020/9/24. // Copyright © 2020 Yang Kejun. All rights reserved. // https://github.com/yangKJ/KJExtensionHandler #import "UILabel+KJExtension.h" #import <objc/runtime.h> @implementation UILabel (KJExtension) - (KJLabelTextAlignmentType)kTextAlignmentType{ return (KJLabelTextAlignmentType)[objc_getAssociatedObject(self, @selector(kTextAlignmentType)) integerValue]; } - (void)setKTextAlignmentType:(KJLabelTextAlignmentType)kTextAlignmentType{ objc_setAssociatedObject(self, @selector(kTextAlignmentType), @(kTextAlignmentType), OBJC_ASSOCIATION_ASSIGN); static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ method_exchangeImplementations(class_getInstanceMethod(self.class, @selector(drawTextInRect:)), class_getInstanceMethod(self.class, @selector(kj_drawTextInRect:))); }); switch (kTextAlignmentType) { case KJLabelTextAlignmentTypeRight: case KJLabelTextAlignmentTypeRightTop: case KJLabelTextAlignmentTypeRightBottom: self.textAlignment = NSTextAlignmentRight; break; case KJLabelTextAlignmentTypeLeft: case KJLabelTextAlignmentTypeLeftTop: case KJLabelTextAlignmentTypeLeftBottom: self.textAlignment = NSTextAlignmentLeft; break; case KJLabelTextAlignmentTypeCenter: case KJLabelTextAlignmentTypeTopCenter: case KJLabelTextAlignmentTypeBottomCenter: self.textAlignment = NSTextAlignmentCenter; break; default: break; } } - (void)kj_drawTextInRect:(CGRect)rect{ switch (self.kTextAlignmentType) { case KJLabelTextAlignmentTypeRight: case KJLabelTextAlignmentTypeLeft: case KJLabelTextAlignmentTypeCenter: [self kj_drawTextInRect:rect]; break; case KJLabelTextAlignmentTypeBottomCenter: case KJLabelTextAlignmentTypeLeftBottom: case KJLabelTextAlignmentTypeRightBottom:{ CGRect textRect = [self textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines]; textRect.origin = CGPointMake(textRect.origin.x, -CGRectGetMaxY(textRect)+rect.size.height); [self kj_drawTextInRect:textRect]; } break; default:{ CGRect textRect = [self textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines]; [self kj_drawTextInRect:textRect]; } break; }} / / / get highly - CGFloat kj_calculateHeightWithWidth: (CGFloat) width {self. NumberOfLines = 0; self.lineBreakMode = NSLineBreakByCharWrapping; CGSize size = [UILabel kj_calculateLabelSizeWithTitle:self.text font:self.font constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:NSLineBreakByCharWrapping]; size.height += 3; return size.height; } // get the height, Specify the line height - (CGFloat) kj_calculateHeightWithWidth: CGFloat width OneLineHeight: (CGFloat) height {self. NumberOfLines = 0; self.lineBreakMode = NSLineBreakByCharWrapping; CGSize size = [UILabel kj_calculateLabelSizeWithTitle:self.text font:self.font constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:NSLineBreakByCharWrapping]; return size.height * height / self.font.lineHeight; } / / / capture text size + CGSize kj_calculateLabelSizeWithTitle: (nsstrings *) the title font: (UIFont *) the font constrainedToSize (CGSize) size lineBreakMode:(NSLineBreakMode)lineBreakMode{ if (title.length == 0) return CGSizeZero; NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; paragraph.lineBreakMode = lineBreakMode; CGRect frame = [title boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraph} context:nil]; return frame.size; } @endCopy the code