我想调整我的UITextView中的文本的大小,使它适合。我有以下代码:基于UITextView帧大小调整文本
NSString *storyTitle = [newsfeedToSubject.properties valueForKey:@"title"];
int currentFontSize = 18;
UIFont *currentFont = [UIFont fontWithName:kProximaNovaBold size:currentFontSize];
CGSize storyTitleSize = [storyTitle sizeWithFont:currentFont constrainedToSize:self.newsfeedStoryTitle_.frameSize lineBreakMode:UILineBreakModeWordWrap];
while (storyTitleSize.height >= self.newsfeedStoryTitle_.frameHeight){
currentFontSize--;
currentFont = [UIFont fontWithName:kProximaNovaBold size:currentFontSize];
storyTitleSize = [storyTitle sizeWithFont:currentFont constrainedToSize:self.newsfeedStoryTitle_.frameSize lineBreakMode:UILineBreakModeWordWrap];
}
[self.newsfeedStoryTitle_ setFont:currentFont];
[self.newsfeedStoryTitle_ setText:storyTitle];
[self.newsfeedStoryTitle_ setBackgroundColor:[UIColor redColor]];
然而,这里就是我得到:
我试着用不同的换行模式,还可以设置自动调整大小为无,但是事实并非如此帮帮我。任何想法?
yea..the问题是我不希望调整框架..我想要调整的文本 – adit 2012-07-31 18:16:21
如何计算换行符的数量? – adit 2012-07-31 18:18:40
请参阅NSString UIKit Additions Reference(https://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html),特别是'sizeWithFont:forWidth:lineBreakMode'方法。这应该会返回一个CGSize,它应该具有适当的字符串高度。我不在我的Mac上进行验证,但它应该能够满足您的需求。也就是说,如果返回的高度大于静态框架,请减少字体大小,然后重试。 – jwest 2012-07-31 18:39:27