Instantly share code, notes, and snippets.
Last active
December 9, 2020 13:07
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
-
Save Axemasta/5fe4de0c4b680411696555bfd4a569de to your computer and use it in GitHub Desktop.
Attributed UIButtons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // 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