UITextView在Rect中绘制文本

时间:2016-12-21 15:06:25

标签: ios uitextview

如何在UITextView中实现绘图文字。实际上我想在UITextView中显示两列,并且正在寻找CoreText。事情是一个字符串说"Hello World".drawTextInRect:rect只是采取矩形。这个字符串将如何知道它必须绘制的UITextView。我需要继承textview吗?

2 个答案:

答案 0 :(得分:0)

听起来你根本不应该使用UITextView,但应该创建自己的UIView自定义子类。

UITextView是一个非常丰富的类,它将绘制属性,滚动文本,以及从用户那里获取输入。

使用text属性创建自己的视图,并实现使用Core Text的drawRect方法,如果这是你需要的。

答案 1 :(得分:0)

这样的子类uiview

import UIKit

class myUIview: UIView {

    var mytxt:UITextView!
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    func addText(str:String) -> Void {
        mytxt=UITextView();
        mytxt.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        mytxt.text=str;
        self.addSubview(mytxt);
    }
}

将此子类添加到您的superview

  let newV:myUIview=myUIview();

        newV.backgroundColor=UIColor.greenColor();
        newV.frame=CGRectMake(200, 200, 100, 100);
          newV.addText("Create your own view with a text property and implement a drawRect method that uses Core Text if that's what you need.");
        self.view?.addSubview(newV);
相关问题