详解button设置文字和图片

详解button设置文字和图片,第1张

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]

button.backgroundColor = kRandomColor

button.tag = i + addTag

button.frame = CGRectMake(kScreenWidth / 3 * i, 0, kScreenWidth / 3, 50)

//首先设置需要显示的文字和图片

[button setTitle:titleArray[i] forState:UIControlStateNormal]

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]

[button setImage:image forState:UIControlStateNormal]

//再将文字和图片进行偏移处理

//这里+3和-3是需要文字和图片有一定的间隔

[button setTitleEdgeInsets:UIEdgeInsetsMake(0, -image.size.width, 0, image.size.width)]

[button setImageEdgeInsets:UIEdgeInsetsMake(0, button.titleLabel.bounds.size.width+3, 0, -button.titleLabel.bounds.size.width-3)]

//自己去将图片进行修改

- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{

// 创建一个bitmap的context

// 并把它设置成为当前正在使用的context

UIGraphicsBeginImageContext(size)

// 绘制改变大小的图片

[img drawInRect:CGRectMake(0, 0, size.width, size.height)]

// 从当前context中创建一个改变大小后的图片

UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext()

// 使当前的context出堆栈

UIGraphicsEndImageContext()

// 返回新的改变大小后的图片

return scaledImage

}

1、执行“幻灯片放映动画按钮自定义”命令。

2、在幻灯片中拖拉出一个按钮来,此时系统自动d出“动作设置”对话框。

3、选中“超链接到”选项,然后单击其右侧的下拉按钮,在随后d出的下拉列表中,选择“幻灯片”选项,打开“超链接到幻灯片”对话框,选中幻灯片,确定返回。

4、右击按钮,在随后d出的快捷菜单中,选择“添加文本”选项,并输入文本。

5、设置好文本的字号字体等,调整好按钮大小,并将其定位在合适的位置上即可。

安卓在Button上添加两行文字

    mian.xml代码:

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView     android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="@string/hello"    />    <LinearLayout    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="vertical"    android:clickable="true"    android:focusable="true"    android:background="@android:drawable/btn_default">    <TextView        android:text="100"        android:layout_width="fill_parent"        android:gravity="center_horizontal"        android:layout_height="wrap_content"        android:layout_weight="1"/>    <TextView        android:text="次数"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center_horizontal"        android:textColor="#FFD700"        android:layout_weight="1"/>   </LinearLayout></LinearLayout

运行效果:


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11411873.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-15
下一篇2023-05-15

发表评论

登录后才能评论

评论列表(0条)

    保存