| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- //
- // UILabel+UILabel_Common.m
- // CommonLibrary
- //
- // Created by AlexiChen on 14-1-18.
- // Copyright (c) 2014年 CommonLibrary. All rights reserved.
- //
- #import "UILabel+Common.h"
- @implementation UILabel (Common)
- + (instancetype)label
- {
- UILabel *label = [[UILabel alloc] init];
- label.textAlignment = NSTextAlignmentLeft;
- label.backgroundColor = [UIColor clearColor];
- label.textColor = [UIColor blackColor];
- return label;
- }
- + (instancetype)labelWithTitle:(NSString *)title
- {
- UILabel *label = [UILabel label];
-
- label.text = title;
- return label;
- }
- - (CGSize)contentSize
- {
- return [self textSizeIn:self.bounds.size];
- }
- - (CGSize)textSizeIn:(CGSize)size
- {
- NSLineBreakMode breakMode = self.lineBreakMode;
- UIFont *font = self.font;
-
- CGSize contentSize = CGSizeZero;
- // if ([IOSDeviceConfig sharedConfig].isIOS7)
- // {
- NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- paragraphStyle.lineBreakMode = breakMode;
- paragraphStyle.alignment = self.textAlignment;
-
- NSDictionary* attributes = @{NSFontAttributeName:font,
- NSParagraphStyleAttributeName:paragraphStyle};
- contentSize = [self.text boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributes context:nil].size;
- // }
- // else
- // {
- // contentSize = [self.text sizeWithFont:font constrainedToSize:size lineBreakMode:breakMode];
- // }
-
-
- contentSize = CGSizeMake((int)contentSize.width + 1, (int)contentSize.height + 1);
- return contentSize;
- }
- - (CGFloat)takeLblHeight:(NSString *)content withTextFontSize:(CGFloat)mFontSize lineSpaceing:(NSInteger)lineSpaceing size:(CGSize)size
- {
- NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- [paragraphStyle setLineSpacing:lineSpaceing];
- NSMutableAttributedString *attributes = [[NSMutableAttributedString alloc] initWithString:content];
- [attributes addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:mFontSize] range:NSMakeRange(0, content.length)];
- [attributes addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, content.length)];
- CGSize attSize = [attributes boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
- self.attributedText = attributes;
- return attSize.height;
- }
- - (void)lblHeight:(CGFloat)lblHeight
- {
- CGRect rect = self.frame;
- rect.size.height = lblHeight;
- self.frame = rect;
- }
- //- (void)layoutInContent
- //{
- // CGSize size = [self contentSize];
- // CGRect rect = self.frame;
- // rect.size = size;
- // self.frame = rect;
- //}
- //
- @end
- @implementation InsetLabel
- - (void)drawTextInRect:(CGRect)rect
- {
- [super drawTextInRect:UIEdgeInsetsInsetRect(rect, _contentInset)];
- }
- - (CGSize)contentSize
- {
- CGRect rect = UIEdgeInsetsInsetRect(self.bounds, _contentInset);
- CGSize size = [super textSizeIn:rect.size];
- return CGSizeMake(size.width + _contentInset.left + _contentInset.right, size.height + _contentInset.top + _contentInset.bottom);
- }
- - (CGSize)textSizeIn:(CGSize)size
- {
- size.width -= _contentInset.left + _contentInset.right;
- size.height -= _contentInset.top + _contentInset.bottom;
- CGSize textSize = [super textSizeIn:size];
- return CGSizeMake(textSize.width + _contentInset.left + _contentInset.right, textSize.height + _contentInset.top + _contentInset.bottom);
- }
- @end
|