Skip to content

Instantly share code, notes, and snippets.

@vinayjn
vinayjn / UIView+LayoutAnchors.swift
Created September 23, 2021 03:22
UIView Layout Anchor Extensions
//
// UIView+LayoutAnchors.swift
//
// Created by Vinay Jain.
//
import UIKit
protocol LayoutAnchor {
func constraint(equalTo anchor: Self,
@vinayjn
vinayjn / draw_line.py
Last active March 12, 2018 18:21
Draw lines
text = "This could be a single line text but its too long to fit in one."
lines = text_wrap(text, font, image_size[0])
line_height = font.getsize('hg')[1]
x = 10
y = 20
for line in lines:
# draw the line on the image
draw.text((x, y), line, fill=color, font=font)
@vinayjn
vinayjn / correct_height.py
Created March 11, 2018 19:31
Correct height calculation
text = "This could be a single line text but it can't fit in one line."
lines = text_wrap(lines, font)
line_height = font.get_size('hg')[1]
print line_height
# Output
# 62
@vinayjn
vinayjn / incorrect_height.py
Created March 11, 2018 19:13
Incorrect height calculation
text = "This could be a single line text but it can't fit in one line."
lines = text_wrap(lines, font)
for line in lines:
print font.getsize(line)[1]
# Output
# 62
# 51
@vinayjn
vinayjn / textwrap.py
Created March 11, 2018 18:50
Split long lines with pillow
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
def text_wrap(text, font, max_width):
lines = []
# If the width of the text is smaller than image width
# we don't need to split it, just add it to the lines array
# and return
if font.getsize(text)[0] <= max_width:
@vinayjn
vinayjn / snippet_2.py
Last active January 24, 2018 13:15
Draw text with Pilllow
# create font object with the font file and specify
# desired size
font = ImageFont.truetype('Roboto-Bold.ttf', size=45)
# starting position of the message
(x, y) = (50, 50)
message = "Happy Birthday!"
color = 'rgb(0, 0, 0)' # black color
@vinayjn
vinayjn / snippet_1.py
Last active December 5, 2017 18:38
Draw text with Pillow
# import required classes
from PIL import Image, ImageDraw, ImageFont
# create Image object with the input image
image = Image.open('background.png')
# initialise the drawing context with
# the image object as background
draw = ImageDraw.Draw(image)
@vinayjn
vinayjn / AnchorPoint.swift
Last active February 10, 2020 15:16
Change the anchor point of a CALayer in Swift
setAnchorPoint(CGPoint(x: 0.5, y: 0.5), forLayer: layer)
private func setAnchorPoint(anchorPoint: CGPoint, forLayer layer: CALayer) {
var newPoint = CGPoint(x: layer.bounds.size.width * anchorPoint.x, y: layer.bounds.size.height * anchorPoint.y)
var oldPoint = CGPoint(x: layer.bounds.size.width * layer.anchorPoint.x, y: layer.bounds.size.height * layer.anchorPoint.y)
newPoint = CGPointApplyAffineTransform(newPoint, layer.affineTransform())
oldPoint = CGPointApplyAffineTransform(oldPoint, layer.affineTransform())
var position = layer.position
position.x -= oldPoint.x
@vinayjn
vinayjn / NSFontAttributeName.m
Last active December 19, 2015 06:44
NSFontAttributeName
- (IBAction)applyItalicAttributes:(UIButton *)sender {
NSDictionary *attributes = @{
NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Italic"
size:30.0]
};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"HelveticaNeue with Italic Attributes"
attributes:attributes];
######################
# Options
######################
REVEAL_ARCHIVE_IN_FINDER=false
FRAMEWORK_NAME="${PROJECT_NAME}"
SIMULATOR_LIBRARY_PATH="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${FRAMEWORK_NAME}.framework"