gpt4 book ai didi

iphone - 根据可用的文本字体大小和字体名称初始化 UITextView

转载 作者:行者123 更新时间:2023-11-29 11:03:58 24 4
gpt4 key购买 nike

这是我试过的,

 UITextView *_textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300, 10)];
NSString *str = @"This is a test text view to check the auto increment of height of a text view. This is only a test. The real data is something different.";
_textView.text = str;


CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;//Here i am adjusting the textview

[self.view addSubview:_textView];



Basically after fitting the text into textview,scrolling is enable,but i cannot view the content inside the textview without scrolling the textview.I do want to initialize the UITextView frame size based on the text size,font name etc.

感谢任何解决方案。谢谢。

最佳答案

NSString *str = @"This is a test text view to check the auto increment of height of a text     view. This is only a test. The real data is something different.";
UIFont * myFont = [UIFont fontWithName:@"your font Name"size:12];//specify your font details here

//然后计算上面文字需要的高度。

CGSize textviewSize = [str sizeWithFont:myFont constrainedToSize:CGSizeMake(300, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];

//根据你从上面得到的高度初始化你的textview

UITextView *_textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, textviewSize.width, textviewSize.height)];
_textView.text = str;
[self.view addSubview:_textView];

而且你还想在 textview 中禁用滚动然后引用这个。

正如 William Jockusch 在 his answer here 中所说的那样:

You can disable almost all scrolling by putting the following method into your UITextView subclass:

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
// do nothing
}

The reason I say "almost" all scrolling is that even with the above, it still accepts user scrolls. Though you could disable those by setting self.scrollEnabled to NO.

If you want to only disable some scrolls, then make an ivar, lets call it acceptScrolls, to determine whether you want to allow scrolling or not. Then your scrollRectToVisible method can look like this:

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
if (self.acceptScrolls)
[super scrollRectToVisible: rect animated: animated];
}

关于iphone - 根据可用的文本字体大小和字体名称初始化 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14534031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com