iOS: Text textAligment support Top and Bottom

This commit is contained in:
pengfei.zhou 2021-09-08 20:19:41 +08:00 committed by osborn
parent 41e610e424
commit 5b3d45f607

View File

@ -26,17 +26,31 @@
#import "Doric.h" #import "Doric.h"
@interface DoricTextView : UILabel @interface DoricTextView : UILabel
@property(nonatomic, assign) DoricGravity gravity;
@end @end
@implementation DoricTextView @implementation DoricTextView
- (void)drawTextInRect:(CGRect)rect { - (void)drawTextInRect:(CGRect)rect {
[super drawTextInRect:UIEdgeInsetsInsetRect( rect = UIEdgeInsetsInsetRect(
rect, rect,
UIEdgeInsetsMake( UIEdgeInsetsMake(
self.doricLayout.paddingTop, self.doricLayout.paddingTop,
self.doricLayout.paddingLeft, self.doricLayout.paddingLeft,
self.doricLayout.paddingBottom, self.doricLayout.paddingBottom,
self.doricLayout.paddingRight))]; self.doricLayout.paddingRight));
if ((self.gravity & DoricGravityTop) == DoricGravityTop) {
CGRect realRect = [self textRectForBounds:rect
limitedToNumberOfLines:self.numberOfLines];
rect.origin.y = self.doricLayout.paddingTop;
rect.size.height = realRect.size.height;
} else if ((self.gravity & DoricGravityBottom) == DoricGravityBottom) {
CGRect realRect = [self textRectForBounds:rect
limitedToNumberOfLines:self.numberOfLines];
rect.origin.y = self.height - realRect.size.height - self.doricLayout.paddingBottom;
rect.size.height = realRect.size.height;
}
rect.size.width = MAX(0.01f,rect.size.width);
[super drawTextInRect:rect];
} }
@end @end
@ -93,6 +107,7 @@ - (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)pro
} else { } else {
view.textAlignment = alignment; view.textAlignment = alignment;
} }
((DoricTextView *) view).gravity = gravity;
} else if ([name isEqualToString:@"maxLines"]) { } else if ([name isEqualToString:@"maxLines"]) {
view.numberOfLines = [prop integerValue]; view.numberOfLines = [prop integerValue];
} else if ([name isEqualToString:@"fontStyle"]) { } else if ([name isEqualToString:@"fontStyle"]) {
@ -289,8 +304,8 @@ - (UIImage *)gradientImageFromColors:(NSArray *)colors
if (@available(iOS 10.0, *)) { if (@available(iOS 10.0, *)) {
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init]; UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
format.scale = [UIScreen mainScreen].scale; format.scale = [UIScreen mainScreen].scale;
UIGraphicsImageRenderer *render = [[UIGraphicsImageRenderer alloc]initWithSize:imgSize format:format]; UIGraphicsImageRenderer *render = [[UIGraphicsImageRenderer alloc] initWithSize:imgSize format:format];
UIImage *image = [render imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) { UIImage *image = [render imageWithActions:^(UIGraphicsImageRendererContext *_Nonnull rendererContext) {
CGContextRef context = rendererContext.CGContext; CGContextRef context = rendererContext.CGContext;
CGContextSaveGState(context); CGContextSaveGState(context);