UITextView Rich text?

前端 未结 3 904
你的背包
你的背包 2021-01-01 03:02

I was wondering if UITextView can display rich text? I want to have simple formatting on my read only text (like different alignment for different parts of the text). if not

3条回答
  •  梦毁少年i
    2021-01-01 03:10

    Here is a code snippet for anyone trying to do this - use the uiwebview to handle styled text - getting your local js files to work can be trick. Just go into Build Phases -> Compile Sources -> and add the js files you want to access locally in your html content. You can also evaluate strings as javascript from objective c on your webview.

    NSString *someHtmlContent = [NSString stringWithFormat:@"", dynamicText]; 
    
    NSLog(@"HTML with code: \n%@", someHtmlContent);
    
    NSString *resourcePath = [[[[NSBundle mainBundle] resourcePath]
                                   stringByReplacingOccurrencesOfString:@"/" withString:@"//"]
                                  stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    
    [MyWebView loadHTMLString:someHtmlContent baseURL:[NSURL URLWithString:
                                                [NSString stringWithFormat:@"file:/%@//", resourcePath]]];
    
    [self.MyWebView setDelegate:self];
    

    Setting the delegate let's you decide how to handle links - do you want them to open in mobile safari or in your webview

提交回复
热议问题