UILabel 富文本 显示文字和图片

//    在UILabel上显示文字和图片
    //UIlabel 富文本 字体显示不同颜色
    UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 30)];
    testLabel.backgroundColor = [UIColor lightGrayColor];
    testLabel.textAlignment = NSTextAlignmentCenter;
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc] initWithString:@"今天天气不错呀"];
    [AttributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0] range:NSMakeRange(2, 2)];
    [AttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 3)];
    testLabel.attributedText = AttributedStr;
    [self.view addSubview:testLabel];
    
    //UIlabel 富文本 图文混排
    UILabel *testLabel_2 =[[UILabel alloc]init];
    testLabel_2.frame =CGRectMake(0, 100+50, 200, 100);
    testLabel_2.backgroundColor =[UICo
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的示例代码,可以在 Unity 和 NGUI 中实现显示带多个网络图片富文本: ```csharp using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; using UnityEngine.UI; public class RichText : MonoBehaviour { public UILabel label; public UIGrid imagesGrid; public GameObject imagePrefab; private List<string> urls = new List<string>(); // 正则表达式匹配网络图片的链接 private static readonly Regex UrlRegex = new Regex(@"\b(?:https?://|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase); public void SetText(string text) { // 清空之前的图片 foreach (Transform child in imagesGrid.transform) { Destroy(child.gameObject); } // 获取所有网络图片的链接 urls.Clear(); MatchCollection matches = UrlRegex.Matches(text); foreach (Match match in matches) { string url = match.Value; if (url.EndsWith(".jpg") || url.EndsWith(".png")) { urls.Add(url); } } // 将所有网络图片添加到 imagesGrid 中 foreach (string url in urls) { GameObject imageObject = Instantiate(imagePrefab, imagesGrid.transform); StartCoroutine(LoadImage(url, imageObject.GetComponent<UITexture>())); } // 将文本内容设置到 labellabel.text = text; } private IEnumerator LoadImage(string url, UITexture texture) { WWW www = new WWW(url); yield return www; if (www.error == null) { texture.mainTexture = www.texture; } else { Debug.LogError("Failed to load image: " + www.error); } } } ``` 使用方法: 1. 在 Unity 中创建一个 UI Root,添加一个 NGUILabel 和 Grid。 2. 将 RichText 脚本挂载到 Label 上,将 imagesGrid 和 imagePrefab 拖拽到脚本对应的字段中。 3. 调用 `SetText()` 方法设置富文本内容即可,例如: ```csharp RichText richText = GetComponent<RichText>(); richText.SetText("这是一段带图片富文本,<img src=\"http://example.com/image1.jpg\"> 和 <img src=\"http://example.com/image2.png\">"); ``` 这样,就可以在 NGUI 中实现显示带多个网络图片富文本了。注意,这里使用了 HTML 标记 `<img>` 来指定图片的链接,这是一种常见的富文本格式。同时,也可以根据需要修改正则表达式来支持不同的富文本格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值