Skip to content

Instantly share code, notes, and snippets.

@wwwins
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wwwins/9384342 to your computer and use it in GitHub Desktop.
Save wwwins/9384342 to your computer and use it in GitHub Desktop.
UITextView issues in iOS6 and iOS7
//
// TextDemoViewController.m
// TextDemo
//
// Created by wwwins on 2014/3/5.
// Copyright (c) 2014年 wwwins. All rights reserved.
//
#import "TextDemoViewController.h"
#define LINE_HEIGHT (25.f)
#define FONT_SIZE (16.f)
@interface TextDemoViewController ()
@end
@implementation TextDemoViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *content = @"近機黃,為意子著又北飛樂間難家為,已市長臺然那個生但麼三區金了帶,小然拿一張五正方空等料女心進分讓就已成些死!不當會那容別,計運色,進空想日、顧便品!十真當效推臺找利卻!事聽政燈,臺將進境服到。候色集把是人背而館小要山怎那千醫這四不取見月德真故利世率、造在可,了告。明手說利前集口星明臺己物力!選如水,有一形續包服老是?照太易手爸體言眼,安常西式英形回中回觀有師陸不,性觀出樂……海三腦,年的文營就技全的各我那有太賽,多區的麼來子是看此明毛怎子而力望山動家的,都簡不了遊。我我中把響急感、兒工到白臺師痛標未,書稱立題屋,大她位大:在出高寫話學了師,何是想代為資同念的指市上;分答到她取覺自多為沒建復者和感都了精關把全半頭熱觀見個美收在好政實是發二員民用全話叫。";
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300, 300)];
[self.view addSubview:textView];
// disable scrolling to show all textview
[textView setScrollEnabled:NO];
// adjust lineSpacing
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.minimumLineHeight = LINE_HEIGHT;
style.maximumLineHeight = LINE_HEIGHT;
style.lineHeightMultiple = LINE_HEIGHT;
// \n 之間的間距
//style.paragraphSpacing = LINE_HEIGHT;
#if 0
// ios7
NSDictionary *attributes = @{
NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:FONT_SIZE],
NSParagraphStyleAttributeName : style,
};
#else
// Bug: NSFontAttributeName not working in ios6
NSDictionary *attributes = @{
NSParagraphStyleAttributeName : style,
};
#endif
[textView setAttributedText:[[NSAttributedString alloc] initWithString:content attributes:attributes]];
// fixed: font-size note updated in ios6
[textView setEditable:YES];
[textView setFont:[UIFont fontWithName:@"Helvetica" size:FONT_SIZE]];
[textView setEditable:NO];
[textView setFrame:CGRectMake(10, 10, 300, [self measureHeightOfUITextView:textView])];
NSLog(@"height:%f", [self measureHeightOfUITextView:textView]);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - fixed uitextview contentSize issue
- (CGFloat)measureHeightOfUITextView:(UITextView *)textView
{
return [textView sizeThatFits:CGSizeMake(textView.frame.size.width, FLT_MAX)].height;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment