TextView设置文字的颜色

1.今天做项目的时候遇到了一个问题,就是无论我怎么设置textView的文字都不管用呀,半天网上找资料呀,这才解决这个问题呀,其实什么问题只要静下心来就可以好好的实现:

以后是实现的代码:

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextView *textview;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

NSMutableAttributedString * attributedTextString = [[NSMutableAttributedString alloc] initWithString:@"hahahahhahahahhiuhgilgliuglugugguglgjlgjgjgljgjgjuguyguydgasjgfglsa,g和环境的还是觉得还是计划经济健康来了来了健康哈哈哈哈哈哈"];

int length = (int)attributedTextString.length;

//设置的是字的背景颜色

//[attributedTextString addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, length)];

//设置字的颜色

[attributedTextString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0 ,length)];

//设置的时字的字体及大小

[attributedTextString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(0, length)];

self.textview.attributedText = attributedTextString;

//给键盘上面加一个done(完成用来取消键盘)

//定义一个toolBar添加一done的完成

UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];

//设置style

[topView setBarStyle:UIBarStyleBlack];

//定义两个flexibleSpace的button,放在toolBar上,这样完成按钮就会在最右边

UIBarButtonItem * button1 =[[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];

UIBarButtonItem * button2 = [[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];

//定义完成按钮

UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone  target:self action:@selector(resignKeyboard)];

//在toolBar上加上这些按钮

NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];

[topView setItems:buttonsArray];

[self.textview setInputAccessoryView:topView];

}

//取消键盘的消失

-(void)resignKeyboard{

[self.textview resignFirstResponder];

}

}

时间: 2024-08-08 02:40:45

TextView设置文字的颜色的相关文章

TextView 多文字字体颜色及点击事件

像微信朋友圈点赞功能如:张三.李四.王五.这种格式 最早做法是在layout中创建一个父类容器如linearlayout然后在创建一个子layout,在代码中 通过for循环addView添加到父类容器当中,虽然可以实现效果但是这样无形中会增加很多view控件. 通过SpannableStringBuilder只通过一个textview就可以实现上面的效果. InvitationLike userLike; SpannableStringBuilder builder = new Spannab

Android TextView 设置文字间距

搜索结果都不靠谱,基本上都说是android:textScaleX可以设置文字间距,也不知道你们到底有没有真正做过开发. Android开发文档里描述的很清楚: android:textScaleX Sets the horizontal scaling factor for the text. 这个东西是用来设置水平方向的文字放大和缩小的. 其实设置文字间距可以通过修改string.xml里的字符,在其中添加空格来实现. 但是在字符串中直接输入空格达不到想要的效果,这就需要使用转义字符来进行转

通过HTML的方式给textview设置不同的颜色

private void setTextViewNum(int betNum) { //通过html的方式来给textview里的文字设置颜色 String textInfo = new StringBuilder() .append("<font color=\"#ffffff\">") .append("共") .append(betNum+"") .append("注") .append(

TextView设置动态改变颜色

通过TextView的setTextColor方法进行文本颜色的设置, 这里可以有3种方式进行设置: 第1种:tv.setTextColor(android.graphics.Color.RED);//系统自带的颜色类 第2种:tv.setTextColor(0xffff00ff);//0xffff00ff是int类型的数据,分组一下0x|ff|ff00ff,0x是代表颜色整数的标记,ff是表示透明度,ff00ff表示颜色,注意:这里ffff00ff必须是8个的颜色表示,不接受ff00ff这种6

同一个TextView设置不同的颜色和大小

//strategy1是一个TextView SpannableStringBuilder builder1 = new SpannableStringBuilder(strategy1.getText().toString()); //设置前景色为蓝色 ForegroundColorSpan blue=new ForegroundColorSpan(Color.BLUE); //改变第0-3个字体颜色为蓝色 builder1.setSpan(blue,0,3, Spanned.SPAN_EXC

给textview设置不同的颜色字

SpannableStringBuilder builder = new SpannableStringBuilder(            info.getRyname().trim() + "(" + info.getSj() + ")回复");            ForegroundColorSpan Span = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.btn_bl

android TextView 设置部分文字背景色和文字颜色

通过SpannableStringBuilder来实现,它就像html里边的<span>元素改变指定文字的文字颜色或背景色 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Str

TextView字体大小及颜色设置

TextView设置文字大小及颜色: 1.1)通过xml配置 <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#FF0000" android:textSize="18sp"/> 1.2)通过代码设置(方式一) TextView textView = new T

(转)TextView 设置背景和文本颜色的问题

在做一个项目,突然遇到如下问题 比如:在color.xml中定义了几个颜色 <color name="white">#FFFFFF</color> <color name="orange">#DF8326</color> 复制代码 又给TextView 设置了字体颜色和背景色 <TextView android:id="@+id/hello" android:textColor="@