我在Storyboard中设置了UITextView.它设置为定义的大小.我希望能够根据文本的大小来更改字体的大小.用户无法编辑文本视图,只需更新一次.知道怎么做吗? 只有UITextField(单行输入)具有adj
static const CGFloat MAX_FONT_SIZE = 16.0;
static const CGFloat MIN_FONT_SIZE = 4.0;
@interface MyViewController ()
// I haven't dealt with Storyboard / Interface builder in years,
// so this is my guess on how you link the GUI to code
@property(nonatomic, strong) IBOutlet UITextView* textView;
- (void)textDidChange:(UITextView*)textView;
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.textView addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
self.textView.font = [UIFont systemFontOfSize:MAX_FONT_SIZE];
}
- (void)textDidChange:(UITextView*)textView
{
// You need to adjust this sizing algorithm to your needs.
// The following is oversimplistic.
self.textView.font = [UIFont systemFontOfSize:MAX(
MAX_FONT_SIZE - textView.text.length,
MIN_FONT_SIZE
)];
}
@end
