我的问题在理论上与此类似:iOS NSAttributedString on UIButton
我希望将我的按钮标题改为:
"带下划线的字符串
一些文字"
这需要以swift和100%以编程方式完成。
我试图通过使用NSMutableAttributedString创建带下划线的部分,然后将其附加到其他文本(带有换行符字符)来尝试这样做。但是,这给了我错误"无法分配类型' Void' ('又名'()')输入' NSMutableAttributedString"
以下代码:
var patientName = NSMutableAttributedString(string:"Patient Name", attributes: underlineAttributes)
var clickforinfomessage = NSMutableAttributedString(string: "\nclick for patient info")
clickforinfomessage = clickforinfomessage.appendAttributedString(patientName)
startVisitButton.setAttributedTitle(clickforinfomessage, forState: .Normal)
答案 0 :(得分:9)
你可以这样做:
let dict1 = [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue]
let attString = NSMutableAttributedString()
attString.appendAttributedString(NSAttributedString(string: "Patient Name\n", attributes: dict1))
attString.appendAttributedString(NSAttributedString(string: "click for patient info", attributes: nil))
startVisitButton.setAttributedTitle(attString, forState: .Normal)
startVisitButton.titleLabel?.numberOfLines = 0
startVisitButton.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
结果将是:
答案 1 :(得分:0)
只需设置按钮标题标签的行数
即可public Intent getNativeShareIntent(final Context context) {
final String defaultChannel = shareObject.getDefaultChannel();
final PackageManager pm = context.getPackageManager();
final Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
sendIntent.setType("image/jpeg");
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<>();
for (int i = 0; i < resInfo.size(); i++) {
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
final Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setPackage(packageName);
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/jpeg");
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm),
ri.getIconResource()));
}
// TODO Implement the method getSaveToGalleryIntent,
// Could be a simple intent linking to activity.
intentList.add(getSaveToGalleryIntent(context));
Intent openInChooser = Intent.createChooser(intentList.remove(0),
"Share");
LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
return openInChooser;
}
private Intent getSaveToGalleryIntent(final Context context, final Uri imgUrl) {
final Intent intent = new Intent(context, SaveToGalleryActivity.class);
intent.putExtra(SaveToGalleryActivity.EXTRA_KEY_CONTENT, imgUrl);
return new LabeledIntent(intent, BuildConfig.APPLICATION_ID,
"Save to gallery",
R.drawable.ic_save_to_gallery);
}
代码视图:
输出: