I would like to embed some text into a circle in ActionScript. I have three problems: I can't get the text to center in the circle, I can't get the text to be center aligned, and I can't get the font to be applied to the text. Regarding the font, I know that it is embedded correctly, as it works on TextFields I create on the stage.
[Embed(source="DAXCOMPL.TTF", fontName="DaxCompact-Light", mimeType='application/x-font', embedAsCFF='false')]
private var MyFont:Class;
public function Bubble(...) {
var myFont:Font = new MyFont();
var myFormat:TextFormat = new TextFormat();
myFormat.size = 20;
myFormat.align = TextFormatAlign.CENTER;
myFormat.font = myFont.fontName;
var circle:Sprite = new Sprite();
var r:int = 30;
var text:TextField = new TextField();
text.text = "Hello world!";
text.wordWrap = true;
text.defaultTextFormat = myFormat;
text.autoSize = TextFieldAutoSize.LEFT;
text.x = -30;
text.y = -30;
circle.graphics.lineStyle(2, 0x000000, 1.0);
circle.graphics.drawCircle(0,0,r);
circle.graphics.endFill();
circle.addChild(text);
circle.x = 75;
circle.y = 450;
addChild(circle);
}
try to initalize the textfield this way:
var text:TextField = new TextField();
text.embedFonts = true; // use embedded font
text.defaultTextFormat = myFormat; // use this command before setting text
text.text = "Hello world!";
text.wordWrap = true;
text.autoSize = TextFieldAutoSize.LEFT;
text.x = -text.textHeight*0.5; //center the textfield after setting text
text.y = -text.textWidth*0.5;
Related
Basically, I want to add an area with an arbitrary text to an image. So the image size get's bigger afterwards. This is what I've come up with:
public partial class ViewController : UIViewController
{
public override void ViewDidLoad()
{
base.ViewDidLoad();
string filename = "TestImage.jpg";
string bundlePath = NSBundle.MainBundle.BundlePath;
string sourcePath = Path.Combine(bundlePath, filename);
string docPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string destinationPath = Path.Combine(docPath, filename);
File.Copy(sourcePath, destinationPath, true);
string testString = "Lorem ipsum dolor sit amet";
this.AddTextToImage(testString, destinationPath);
var imageView = new UIImageView(new CGRect(0, 0, 500, 500));
imageView.ContentMode = UIViewContentMode.ScaleAspectFit;
imageView.Image = UIImage.FromFile(destinationPath);
this.View.AddSubview(imageView);
this.View.BackgroundColor = UIColor.Red;
}
public void AddTextToImage(string texttoadd, string filepath)
{
UIImage image = UIImage.FromFile(filepath);
nfloat fontSize = 16;
nfloat fWidth = image.Size.Width;
nfloat fHeight = image.Size.Height;
nfloat textWidth;
nfloat textHeight;
CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
UIFont font = UIFont.FromName("Helvetica", fontSize);
NSParagraphStyle style = new NSMutableParagraphStyle();
style.LineBreakMode = UILineBreakMode.WordWrap;
NSAttributedString attributedString = new NSAttributedString(texttoadd, font: font, foregroundColor: UIColor.Blue, paragraphStyle: style);
CGRect stringSize = attributedString.GetBoundingRect(new CGSize(fWidth, double.MaxValue), NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading, null);
textWidth = (nfloat)Math.Ceiling(stringSize.Width);
textHeight = (nfloat)Math.Ceiling(stringSize.Height);
nfloat fullWidth = fWidth;
nfloat fullHeight = fHeight + textHeight;
UIImage composition;
using (CGBitmapContext ctx = new CGBitmapContext(IntPtr.Zero, (nint)fullWidth, (nint)fullHeight, 8, 4 * (nint)fullWidth, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst))
{
CGRect frameRect = new CGRect(0, 0, fullWidth, fullHeight);
ctx.SetFillColor(UIColor.Yellow.CGColor);
ctx.FillRect(frameRect);
CGRect imageRect = new CGRect(0, textHeight, (double)fWidth, (double)fHeight);
ctx.DrawImage(imageRect, image.CGImage);
CGPath stringPath = new CGPath();
stringPath.AddRect(new CGRect(0, 0, textWidth, textHeight));
CTFramesetter framesetter = new CTFramesetter(attributedString);
CTFrame frame = framesetter.GetFrame(new NSRange(0, attributedString.Length), stringPath, null);
frame.Draw(ctx);
using (var imageRef = ctx.ToImage())
composition = new UIImage(imageRef);
}
NSData data = composition.AsJPEG();
NSError error;
data.Save(filepath, NSDataWritingOptions.FileProtectionNone, out error);
}
}
Currently, I have the following issues:
Text is cropped (e.g. fontSize = 160;). Multi line text seems not working.
Text isn't shown at all (e.g. fontSize = 16;).
You can provide answers in Objective-C, Swift or C# - I'll try to translate it.
It seems the font was the problem. Using
UIFont font = UIFont.SystemFontOfSize(fontSize);
now does the job without cutting off text. The only question remaining is, why?
In my spreadsheet, the cell at reference point (28, 9) contains a hyperlink. The code then copies the contents of that cell to another sheet in the same spreadsheet. Right now it will copy the text, but the new cell only contains the text and not the hyperlink properties. Is there a way to preserve the hyperlink through the transfer? I tried looking up hyperlink information, but could not find an answer...or at least not one I understood. I have only been coding for about 2 weeks, so still not understanding a lot. Here is my code:
function submitButtonClick() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
Logger.log('sheet.getName(): ' + sheet.getName());
if (sheet.getName() !== "SubmitReceipt") {return;};
var targetSheet = ss.getSheetByName("ReceiptRecord");
var arrayOfData = [];
var week = sheet.getRange(6,9).getValue();
var emplN = sheet.getRange(4,9).getValue();
var purDate = sheet.getRange(9,9).getValue();
var purFrom = sheet.getRange(11,9).getValue();
var custC = sheet.getRange(14,9).getValue();
var deptC = sheet.getRange(16,9).getValue();
var lotC = sheet.getRange(18,9).getValue();
var laborC = sheet.getRange(20,9).getValue();
var itemC = sheet.getRange(22,9).getValue();
var hyperL = sheet.getRange(28,9).getValue();
var notes = sheet.getRange(44,8).getValue();
arrayOfData[0] = week;
arrayOfData[1] = emplN;
arrayOfData[2] = purDate;
arrayOfData[3] = purFrom;
arrayOfData[4] = custC;
arrayOfData[5] = deptC;
arrayOfData[6] = lotC;
arrayOfData[7] = laborC;
arrayOfData[8] = itemC;
arrayOfData[9] = notes;
arrayOfData[10] = hyperL;
Logger.log('arrayOfData '+ arrayOfData)
var lastRow = targetSheet.getLastRow();
Logger.log('lastRow: ' + lastRow);
Logger.log('arraylength ' + arrayOfData.length);
targetSheet.getRange(lastRow+1, 1, 1, arrayOfData.length).setValues([arrayOfData]);
sheet.getRange(9,9).clearContent();
sheet.getRange(11,9).clearContent();
sheet.getRange(14,9).clearContent();
sheet.getRange(16,9).clearContent();
sheet.getRange(18,9).clearContent();
sheet.getRange(20,9).clearContent();
sheet.getRange(22,9).clearContent();
sheet.getRange(28,9).clearContent();
sheet.getRange(44,8).clearContent();
}
Thank you in advance for your help.
UPDATE: I did find a possible solution and tried it, but then got an error "TypeError: Cannot find function getFormulaR1C1 in object Sample Receipt 6.jpg. (line 26, file "Submit to Record")."
Here is the updated line 26 to show the new solution I tried:
var url = hyperL.getFormulaR1C1();
Thanks again for helping.
I figured it out. Notice the differences between the "//Changes" comments.
function submitButtonClick() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
Logger.log('sheet.getName(): ' + sheet.getName());
if (sheet.getName() !== "SubmitReceipt") {return;};
var targetSheet = ss.getSheetByName("ReceiptRecord");
var arrayOfData = [];
var week = sheet.getRange(6,9).getValue();
var emplN = sheet.getRange(4,9).getValue();
var purDate = sheet.getRange(9,9).getValue();
var purFrom = sheet.getRange(11,9).getValue();
var custC = sheet.getRange(14,9).getValue();
var deptC = sheet.getRange(16,9).getValue();
var lotC = sheet.getRange(18,9).getValue();
var laborC = sheet.getRange(20,9).getValue();
var itemC = sheet.getRange(22,9).getValue();
var hyperL = sheet.getRange(28,9).getValue();
var notes = sheet.getRange(44,8).getValue();
//Changes
var range = SpreadsheetApp.getActiveSheet().getRange(28,9,1,3);
Logger.log('range= ' + range.getValue());
Logger.log('hyperlink: ' + range.getFormulaR1C1());
var url = /"(.*?)"/.exec(range.getFormulaR1C1())[1];
Logger.log('url: ' + url);
//Changes
arrayOfData[0] = week;
arrayOfData[1] = emplN;
arrayOfData[2] = purDate;
arrayOfData[3] = purFrom;
arrayOfData[4] = custC;
arrayOfData[5] = deptC;
arrayOfData[6] = lotC;
arrayOfData[7] = laborC;
arrayOfData[8] = itemC;
arrayOfData[9] = notes;
arrayOfData[10] = hyperL;
arrayOfData[11] = url;
Logger.log('arrayOfData '+ arrayOfData)
var lastRow = targetSheet.getLastRow();
Logger.log('lastRow: ' + lastRow);
Logger.log('arraylength ' + arrayOfData.length);
targetSheet.getRange(lastRow+1, 1, 1, arrayOfData.length).setValues([arrayOfData]);
sheet.getRange(9,9).clearContent();
sheet.getRange(11,9).clearContent();
sheet.getRange(14,9).clearContent();
sheet.getRange(16,9).clearContent();
sheet.getRange(18,9).clearContent();
sheet.getRange(20,9).clearContent();
sheet.getRange(22,9).clearContent();
sheet.getRange(28,9).clearContent();
sheet.getRange(44,8).clearContent();
}
I don't know how to set partial text as a Bold to LabelField in Blackberry.
Kindly refer the attached image,
This example should contain everything you'll need. Your code should look something like this:
String copyText = "Tap NEXT to proceed";
Font[] fonts = new Font[2];
fonts[0] = Font.getDefault();
fonts[1] = Font.getDefault().derive(Font.BOLD);
int[] offsets = new int[4];
offsets[0] = 0;
offsets[1] = "Tap ".length
offsets[2] = "Tap NEXT".length;
offsets[3] = copyText.length();
byte[] attributes = new byte[3];
attributes[0] = 0;
attributes[1] = 1;
attributes[2] = 0;
RichTextField textField = new RichTextField(copyText, offsets, attributes, fonts, NON_FOCUSABLE);
add(textField);
I have the following MonoTouch code which can change the Saturation , but I am trying to also change the Hue.
float hue = 0;
float saturation = 1;
if (colorCtrls == null)
colorCtrls = new CIColorControls() {
Image = CIImage.FromCGImage (originalImage.CGImage) };
else
colorCtrls.Image = CIImage.FromCGImage(originalImage.CGImage);
colorCtrls.Saturation = saturation;
var output = colorCtrls.OutputImage;
var context = CIContext.FromOptions(null);
var result = context.CreateCGImage(output, output.Extent);
return UIImage.FromImage(result);
It's part of a different filter so you'll need to use CIHueAdjust instead of CIColorControls to control the hue.
Here's what I ended up doing to add Hue:
var hueAdjust = new CIHueAdjust() {
Image = CIImage.FromCGImage(originalImage.CGImage),
Angle = hue // Default is 0
};
var output = hueAdjust.OutputImage;
var context = CIContext.FromOptions(null);
var cgimage = context.CreateCGImage(output, output.Extent);
return UIImage.FromImage(cgimage);
However, this does not work on Retina devices, the image returned is scaled incorrectly.
I am trying-out Derobins WMD Editor but would like to know where I can remove some of the buttons at the header since I don't need them in my PHP app.
Buttons like image, anchor link etc.
In the wmd.js file, there is a function called makeSpritedButtonRow which is used to create each of the buttons on the menu. This is located around line 913 of the file.
The code to create each button is grouped together and to disable a button you need to comment out the code block for the button you want to remove.
Example
Below is an extract from the top of the function.
var makeSpritedButtonRow = function(){
var buttonBar = document.getElementById("wmd-button-bar");
var normalYShift = "0px";
var disabledYShift = "-20px";
var highlightYShift = "-40px";
var buttonRow = document.createElement("ul");
buttonRow.id = "wmd-button-row";
buttonRow = buttonBar.appendChild(buttonRow);
var boldButton = document.createElement("li");
boldButton.className = "wmd-button";
boldButton.id = "wmd-bold-button";
boldButton.title = "Strong <strong> Ctrl+B";
boldButton.XShift = "0px";
boldButton.textOp = command.doBold;
setupButton(boldButton, true);
buttonRow.appendChild(boldButton);
To disable the bold button in your application, comment out the code block for the boldButton so it looks like the code below.
var makeSpritedButtonRow = function(){
var buttonBar = document.getElementById("wmd-button-bar");
var normalYShift = "0px";
var disabledYShift = "-20px";
var highlightYShift = "-40px";
var buttonRow = document.createElement("ul");
buttonRow.id = "wmd-button-row";
buttonRow = buttonBar.appendChild(buttonRow);
// var boldButton = document.createElement("li");
// boldButton.className = "wmd-button";
// boldButton.id = "wmd-bold-button";
// boldButton.title = "Strong <strong> Ctrl+B";
// boldButton.XShift = "0px";
// boldButton.textOp = command.doBold;
// setupButton(boldButton, true);
// buttonRow.appendChild(boldButton);
This will remove the button.