Skip to content

Instantly share code, notes, and snippets.

@Axemasta
Last active December 9, 2020 13:07
Show Gist options
  • Select an option

  • Save Axemasta/5fe4de0c4b680411696555bfd4a569de to your computer and use it in GitHub Desktop.

Select an option

Save Axemasta/5fe4de0c4b680411696555bfd4a569de to your computer and use it in GitHub Desktop.
Attributed UIButtons
using System;
using CoreGraphics;
using CoreText;
using Foundation;
using UIKit;
namespace RainbowButtons.iOS
{
public partial class ViewController : UIViewController
{
public ViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
//SetNormalTitle();
//SetFancyTitle();
SetRainbowTitle();
}
void SetNormalTitle()
{
Button.SetTitle("Ayup Lads", UIControlState.Normal);
}
void SetFancyTitle()
{
var normalTitle = new NSAttributedString("Click Here", new UIStringAttributes() { ForegroundColor = UIColor.Blue });
var highlightedTitle = new NSAttributedString("I am clicked :D", new UIStringAttributes() { ForegroundColor = UIColor.Green });
Button.SetAttributedTitle(normalTitle, UIControlState.Normal);
Button.SetAttributedTitle(highlightedTitle, UIControlState.Highlighted);
}
void SetRainbowTitle()
{
var rainbowTitle = new NSMutableAttributedString("Rainbow");
rainbowTitle.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Red, new NSRange(0, 1));
rainbowTitle.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Orange, new NSRange(1, 1));
rainbowTitle.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Yellow, new NSRange(2, 1));
rainbowTitle.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Green, new NSRange(3, 1));
rainbowTitle.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Cyan, new NSRange(4, 1));
rainbowTitle.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Blue, new NSRange(5, 1));
rainbowTitle.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Purple, new NSRange(6, 1));
Button.SetAttributedTitle(rainbowTitle, UIControlState.Normal);
}
}
}
//
// ViewController.swift
// AttributedButton
//
// Created by Alexander Duffell on 09/12/2020.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// SetNormalTitle()
// SetFancyTitle()
SetRainbowTitle()
}
func SetNormalTitle() {
myButton.setTitle("Ayup Lads", for: .normal)
}
func SetFancyTitle() {
// .Selected
let mySelectedAttributedTitle = NSAttributedString(string: "Click Here",
attributes: [NSAttributedString.Key.foregroundColor : UIColor.green])
myButton.setAttributedTitle(mySelectedAttributedTitle, for: .highlighted)
// .Normal
let myNormalAttributedTitle = NSAttributedString(string: "Click Here",
attributes: [NSAttributedString.Key.foregroundColor : UIColor.blue])
myButton.setAttributedTitle(myNormalAttributedTitle, for: .normal)
}
func SetRainbowTitle() {
let rainbowTitle = NSMutableAttributedString(string: "Rainbow")
rainbowTitle.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: _NSRange(location: 0, length: 1))
rainbowTitle.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.orange, range: _NSRange(location: 1, length: 1))
rainbowTitle.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.yellow, range: _NSRange(location: 2, length: 1))
rainbowTitle.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.green, range: _NSRange(location: 3, length: 1))
rainbowTitle.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.cyan, range: _NSRange(location: 4, length: 1))
rainbowTitle.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: _NSRange(location: 5, length: 1))
rainbowTitle.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.purple, range: _NSRange(location: 6, length: 1))
myButton.setAttributedTitle(rainbowTitle, for: .normal)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment