UITextView - 修改选定的文本

发布于 2024-11-18 21:19:36 字数 87 浏览 0 评论 0原文

我有一个 UITextView,我想选择该文本的某个部分并修改其样式。例如更改颜色、使其变为斜体或粗体、增加字体大小或更改字体系列。

有帮助吗?

I have a UITextView and i want to select a certain part of this text and modify its style. Like changing the color, making it italic or bold, increasing the font size, or changing the font family.

Any Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对你再特殊 2024-11-25 21:19:36

是的。等待iOS 5。我认为有关iOS 5的大部分信息仍处于NDA状态,所以我们不能在这里讨论。

或者使用 CoreText 自行开发。

Yes. Wait for iOS 5. I think most of the info about iOS 5 is still under NDA, so we can't discuss it here.

Or develop it all yourself with CoreText.

独享拥抱 2024-11-25 21:19:36

您可以使用一些替代方法,如下所示:-

  • 首先采用滚动视图并添加标签或另一个具有不同样式的文本视图,如您想要的粗体字体或 chagne 颜色,然后添加为滚动视图中的子视图。
  • 对于在滚动视图中添加的文本视图,您应该将该文本视图的框架设置为与文本视图中的文本或行一样大,以停止在文本视图中滚动,因为我们已经在滚动视图中添加了文本视图以获得更好的外观。
  • 因此,根据需要获取尽可能多的文本视图、标签或图像,然后添加到滚动视图中,事情非常简单,您可以使用界面生成器来完成此操作。只需要根据您拥有的子视图为滚动视图定义完美的内容大小即可。

You can use some alternative things as follows:-

  • First take a scrollview and add a label or another textview with different style as you want with bold font or chagne color and add as subview in scrollview.
  • For textviews added in scrollview, you should take a frame of that textview as larger as text or lines in textview for stop scrolling in textview because we already added textview in scrollview for better look.
  • So,Take as many textviews or labels or images as your need and add then to the scrollview things are very simple you can do this using an Interface builder. Just need to define a perfect content size for scrollview depends on subviews you have.
深巷少女 2024-11-25 21:19:36

您应该使用shouldChangeTextInRange。这是因为您需要范围来定位要更改文本的位置。
UITextView 允许您将样式保存在 textView.attributedString 中。
解决方案是获取 textView 的属性字符串,并将其中所需的子字符串替换为已更改或样式化的文本。

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{

     NSMutableAttributedString *textViewText = [[NSMutableAttributedString alloc]initWithAttributedString:textView.attributedText];   //gets textView style string

     NSRange selectedTextRange = [textView selectedRange];
     NSString *selectedString = [textView textInRange:textView.selectedTextRange]; //our selected text 

    //lets say you always want to make selected text bold
    UIFont *boldFont = [UIFont boldSystemFontOfSize:self.txtNote.font.pointSize];

    NSDictionary *boldAttr = [NSDictionary dictionaryWithObject:boldFont forKey:NSFontAttributeName];

    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]initWithString:selectedString attributes:boldAttr]; //make attributed string of our selectedtext

    [textViewText replaceCharactersInRange:range withAttributedString:attributedText]; // replace
    textView.attributedText = textViewText;

    return false;
}

You should use shouldChangeTextInRange. This is because you need range to locate the position where you want the text changed.
UITextView lets you save your style in textView.attributedString.
The solution is to get textView's Attributed String and replace the desired sub string in it with changed or styled text.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{

     NSMutableAttributedString *textViewText = [[NSMutableAttributedString alloc]initWithAttributedString:textView.attributedText];   //gets textView style string

     NSRange selectedTextRange = [textView selectedRange];
     NSString *selectedString = [textView textInRange:textView.selectedTextRange]; //our selected text 

    //lets say you always want to make selected text bold
    UIFont *boldFont = [UIFont boldSystemFontOfSize:self.txtNote.font.pointSize];

    NSDictionary *boldAttr = [NSDictionary dictionaryWithObject:boldFont forKey:NSFontAttributeName];

    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]initWithString:selectedString attributes:boldAttr]; //make attributed string of our selectedtext

    [textViewText replaceCharactersInRange:range withAttributedString:attributedText]; // replace
    textView.attributedText = textViewText;

    return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文