[오픈소스] UILabel+FontSizeFit
2016. 11. 28. 15:10ㆍ개발자료/iOS
반응형
Ref : https://github.com/ShingoFukuyama/UILabel-FontSizeToFit/tree/master/Objective-C/UILabel_FontSizeToFitObjC
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithWhite:0.1f alpha:1.0f]; self.words = @[@"apple", @"orange", @"banana", @"grape", @"kiwi", @"mango", @"lychee", @"lemon", @"coconut", @"pear", @"pineapple", @"peach", @"strawberry", @"fig", @"lime", @"papaya", @"nectarine", @"plum", @"quince", @"raspberry", @"ohajiki", @"watermelon", @"blackberry", @"cherry", @"avocado"]; // Setup UILabel UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 100.0f)]; self.label = label; label.numberOfLines = 0; label.textAlignment = NSTextAlignmentCenter; label.text = self.words[0]; label.font = [UIFont systemFontOfSize:50.0f]; label.textColor = [UIColor whiteColor]; label.layer.borderWidth = 2.0; label.layer.borderColor = [UIColor redColor].CGColor; label.layer.cornerRadius = 3.0f; label.lineBreakMode = NSLineBreakByWordWrapping; [self.view addSubview:label]; label.center = self.view.center; // Start Demo Loop NSTimer *timer = [NSTimer timerWithTimeInterval:0.7 target:self selector:@selector(addWord1) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; } - (void)addWord1 { // Simply add word to label NSInteger random = arc4random() % self.words.count; NSString *fruit = self.words[random]; NSString *newText = [NSString stringWithFormat:@"%@ %@", self.label.text, fruit]; self.label.text = newText; [self.label fontSizeToFit]; } - (void)addWord2 { // Add word which with lineSpacing NSInteger random = arc4random() % self.words.count; NSString *fruit = self.words[random]; NSString *newText = [NSString stringWithFormat:@"%@ %@", self.label.text, fruit]; NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; paragraph.lineSpacing = 5.0f; NSDictionary *attributes = @{NSParagraphStyleAttributeName:paragraph}; NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:newText attributes:attributes]; self.label.attributedText = attributedText; [self.label fontSizeToFit]; }
반응형
'개발자료 > iOS' 카테고리의 다른 글
NSArray - NSPredicate (0) | 2017.01.10 |
---|---|
[Debugging] Remote Virtual Interface Tool(rvictl)을 이용한 iOS 디바이스 네트워크 패킷 캡쳐 (0) | 2016.11.30 |
파일별 ARC 적용 (0) | 2016.10.31 |
NSDictionary NSLog 한글 출력 (0) | 2016.09.28 |
ARC 사용 여부 체크 (0) | 2016.06.13 |