简体   繁体   中英

UITextView Draw Text in Rect

How can I implement draw text in UITextView . Actually I want to show two columns in UITextView and was looking for CoreText. The thing is a string say "Hello World".drawTextInRect:rect is only taking the rect. How it will be known to this string about the UITextView where it has to draw. Do I need to subclass textview?

It sounds like you should not use a UITextView at all, but should create your own custom subclass of UIView.

UITextView is a pretty rich class will draw attributed, scrolling text, plus take input from the user.

Create your own view with a text property and implement a drawRect method that uses Core Text if that's what you need.

subclass uiview like this

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);
    }
}

add this subclass to your 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM